﻿using Service.DAL;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Service
{
    public class RingtoneService : IRingtone
    {
        RingtoneDAL ringtoneDAL;
        public RingtoneService()
        {
            ringtoneDAL = new RingtoneDAL();
        }

        public List<Ringtone> GetNewRingtone(Category category, int number)
        {
            using (SqlDataReader reader = ringtoneDAL.GetNewRingtone(category, number))
            {
                List<Ringtone> ringList = new List<Ringtone>();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Ringtone ringtone = new Ringtone();
                        ringtone.Id = long.Parse(reader["Id"].ToString());
                        ringtone.Code = long.Parse(reader["Code"].ToString());
                        ringtone.Title = reader["Title"].ToString();
                        ringtone.Artist = reader["Artist"].ToString();
                        ringtone.Category = reader["Category"].ToString();
                        ringtone.Url = new Uri(reader["Url"].ToString(), UriKind.Absolute);
                        ringtone.Download = long.Parse(reader["Download"].ToString());
                        ringtone.Size = int.Parse(reader["Size"].ToString());
                        ringList.Add(ringtone);
                    }
                }
                return ringList;
            }
        }

        public ID GetNewId(Category category)
        {
            ID id = new ID();
            id.Category = category;
            id.Id = ringtoneDAL.GetNewId(category);
            return id;
        }

        public long GetMaxId()
        {
            return ringtoneDAL.GetMaxId();
        }

        public long GetLastId(Category category)
        {
            return ringtoneDAL.GetLastId(category);
        }

        public List<Ringtone> GetNextRingtone(Category category, long currentId, int number)
        {
            using (SqlDataReader reader = ringtoneDAL.GetNextRingtone(category, currentId, number))
            {
                List<Ringtone> ringList = new List<Ringtone>();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Ringtone ringtone = new Ringtone();
                        ringtone.Id = long.Parse(reader["Id"].ToString());
                        ringtone.Code = long.Parse(reader["Code"].ToString());
                        ringtone.Title = reader["Title"].ToString();
                        ringtone.Artist = reader["Artist"].ToString();
                        ringtone.Category = reader["Category"].ToString();
                        ringtone.Url = new Uri(reader["Url"].ToString(), UriKind.Absolute);
                        ringtone.Download = long.Parse(reader["Download"].ToString());
                        ringtone.Size = int.Parse(reader["Size"].ToString());
                        ringList.Add(ringtone);
                    }
                }
                return ringList;
            }
        }

        public List<Ringtone> Search(string keyword, long currentId, int number)
        {
            using (SqlDataReader reader = ringtoneDAL.Search(keyword, currentId, number))
            {
                List<Ringtone> ringList = new List<Ringtone>();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Ringtone ringtone = new Ringtone();
                        ringtone.Id = long.Parse(reader["Id"].ToString());
                        ringtone.Code = long.Parse(reader["Code"].ToString());
                        ringtone.Title = reader["Title"].ToString();
                        ringtone.Artist = reader["Artist"].ToString();
                        ringtone.Category = reader["Category"].ToString();
                        ringtone.Url = new Uri(reader["Url"].ToString(), UriKind.Absolute);
                        ringtone.Download = long.Parse(reader["Download"].ToString());
                        ringtone.Size = int.Parse(reader["Size"].ToString());
                        ringList.Add(ringtone);
                    }
                }
                return ringList;
            }
        }


        public bool IncreaseDownload(long code)
        {
            return ringtoneDAL.IncreaseDownload(code);
        }
    }
}
